package com.everyline.baselibrary.plate

import android.content.Context
import android.graphics.Color
import android.text.TextUtils
import android.util.AttributeSet
import android.util.Log
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import com.blankj.utilcode.util.StringUtils
import com.everyline.baselibrary.R
import kotlin.math.log

/**
 * Created by on 2021/2/7.
 * 天天挂线
 *  746608431@qq.com
 *  描述:TODO
 */

class PlateView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyle: Int = 0
) : LinearLayout(context, attrs, defStyle) {

    private var tv_0: TextView? = null
    private var tv_1: TextView? = null
    private var tv_2: TextView? = null
    private var tv_3: TextView? = null
    private var tv_4: TextView? = null
    private var tv_5: TextView? = null
    private var tv_6: TextView? = null
    private var tv_7: TextView? = null
    private var tv_8: TextView? = null

    private var plateTextList = arrayListOf<TextView>()

    // 当前选中的 索引
    private var currentIndex = -1
    private lateinit var mPlateInputView: PlateInputView


    init {
        val view = LayoutInflater.from(context).inflate(R.layout.layout_plate_view, this)

        tv_0 = view.findViewById<TextView>(R.id.tv_0)
        tv_1 = view.findViewById<TextView>(R.id.tv_1)
        tv_2 = view.findViewById<TextView>(R.id.tv_2)
        tv_3 = view.findViewById<TextView>(R.id.tv_3)
        tv_4 = view.findViewById<TextView>(R.id.tv_4)
        tv_5 = view.findViewById<TextView>(R.id.tv_5)
        tv_6 = view.findViewById<TextView>(R.id.tv_6)
        tv_7 = view.findViewById<TextView>(R.id.tv_7)
        tv_8 = view.findViewById<TextView>(R.id.tv_8)





        initView()
    }

    private fun initView() {
        initPlateChar()
        initPlateInputView()
    }

    private fun initPlateInputView() {
        mPlateInputView = PlateInputView(context) { word ->

            // 点击删除按钮
            if (isDeleteButton(word)) {
                plateCharBack(plateTextList[currentIndex])

                if (tv_7!!.visibility == View.VISIBLE) {
                    tv_7!!.visibility = View.GONE
                    tv_7!!.setText("")
                    tv_8!!.visibility = View.VISIBLE
                }
                return@PlateInputView
            }

            plateCharForward(plateTextList[currentIndex], word)
        }

        tv_8!!.setOnClickListener {
            tv_7!!.visibility = View.VISIBLE
            tv_8!!.visibility = View.GONE
            tv_7!!.text = ""
            (0..6).forEach { index ->
                if (StringUtils.isEmpty(plateTextList[index].text.toString())) {
                    currentIndex = index
                    showPlateInputView()
                    changeStyle()
                    return@setOnClickListener
                }


            }
            currentIndex = 7
            showPlateInputView()
            changeStyle()

        }

    }

    // 动态生成 车牌号码每一个 TextView
    private fun initPlateChar() {

        plateTextList.add(tv_0!!)
        plateTextList.add(tv_1!!)
        plateTextList.add(tv_2!!)
        plateTextList.add(tv_3!!)
        plateTextList.add(tv_4!!)
        plateTextList.add(tv_5!!)
        plateTextList.add(tv_6!!)
        plateTextList.add(tv_7!!)


        for ((index, mTv) in plateTextList.withIndex()) {
            mTv!!.tag = index
            mTv.setOnClickListener {
                showInputView(it)
            }
        }
    }

    private fun showInputView(it: View) {
        currentIndex = it.tag as Int
        showPlateInputView()
        changeStyle()
    }

    private fun showPlateInputView() {
        mPlateInputView.showAtLocation(this, Gravity.BOTTOM, 0, 0)
        when (currentIndex) {
            0 -> mPlateInputView.showProvince()
            else -> mPlateInputView.showPlateChar()
        }
    }

    private fun changeStyle() {
        plateTextList.forEachIndexed { index, textView ->
            if (isFocus(index)) {
                setTextViewFocusStyle(textView)
            } else {
                resetTextViewStyle(textView)
            }
        }
    }

    /**
     * 设置textView选中样式
     */
    private fun setTextViewFocusStyle(textView: TextView) {
        textView.setBackgroundResource(R.drawable.shape_red_bg)
        textView.setTextColor(context.resources.getColor(R.color.colorPrimary))
    }

    /**
     *  重置textView 样式
     */
    private fun resetTextViewStyle(textView: TextView) {
        textView.setBackgroundResource(R.drawable.shape_bg)
        textView.setTextColor(Color.BLACK)
    }

    private fun isFocus(index: Int) = index == currentIndex

    private fun isDeleteButton(word: String): Boolean = word == PlateInputView.DELETE

    private fun plateCharForward(textView: TextView, content: String) {

        if (isFirstChar()) {
            mPlateInputView.showPlateChar()
        }

        resetTextViewStyleAndText(textView, content)
        Log.d("currentIndex", "$currentIndex")
        if (tv_7!!.visibility == View.VISIBLE) {
            if (currentIndex == 7) {
                mPlateInputView.dismiss()
                return
            }


        } else if (currentIndex == 6) {
            mPlateInputView.dismiss()
            return
        }

        currentIndex++
        setTextViewFocusStyle(plateTextList[currentIndex])
    }

    private fun plateCharBack(textView: TextView) {
        if (isSecondChar()) {
            mPlateInputView.showProvince()
        }

        resetTextViewStyleAndText(textView, "")

        if (isZeroChar()) {
            return
        }

        currentIndex--
        setTextViewFocusStyle(plateTextList[currentIndex])
    }

    private fun resetTextViewStyleAndText(textView: TextView, content: String) {
        textView.text = content
        resetTextViewStyle(textView)
    }

    private fun isZeroChar() = currentIndex - 1 < 0

    private fun isSecondChar() = currentIndex == 1
    private fun isFirstChar() = currentIndex == 0


    fun setPlate(plate: String) {
        if (!TextUtils.isEmpty(plate)) {
            if (plate.length == 7) {
                tv_7!!.visibility = View.GONE
                tv_8!!.visibility = View.VISIBLE

            } else if (plate.length == 8) {
                tv_7!!.visibility = View.VISIBLE
                tv_8!!.visibility = View.GONE
            }

            for (index in plate.indices) {
                plateTextList[index].text = plate[index].toString()
            }
        } else {
            for ((index, mtv) in plateTextList.withIndex()) {
                mtv.text = ""
            }
        }


    }

    fun getPlate(): String {
        val sb = StringBuffer()
        var plateString: String = ""
        if (tv_7!!.visibility == View.VISIBLE) {

            (0..7).forEach { index ->
                plateString = sb.append(plateTextList[index].text).toString()

            }
        } else {

            (0..6).forEach { index ->
                plateString = sb.append(plateTextList[index].text).toString()

            }
        }
        return plateString.toUpperCase()
    }


}